package com.regal.reg;

import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;

public class ItemComboBox extends JComboBox {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public ItemComboBox() {
		super();
		setModel(new ItemModel());
	}

	public void setModel(ItemModel m) {
		if (!(m instanceof ItemModel))
			throw new IllegalArgumentException();
		super.setModel(m);
	}

	public void addItem(String id, String name) {
		super.addItem(name);
		((ItemModel) getModel()).addElement(id, name);
	}

	public String getSelectedId() {
		if (this.getSelectedIndex() != -1) {
			return ((ItemModel) getModel()).getIdAt(this.getSelectedIndex());
		} else {
			return null;
		}
	}

	public void removeAllData() {
		this.removeAllItems();
		((ItemModel) getModel()).removeAllData();
	}

	class ItemModel extends DefaultComboBoxModel {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		class CoupleItem {
			String id;

			String name;

			public CoupleItem(String _id, String _name) {
				this.id = _id;
				this.name = _name;
			}

			public String getId() {
				return id;
			}

			public void setId(String _id) {
				this.id = _id;
			}

			public String getName() {
				return name;
			}

			public void setName(String name) {
				this.name = name;
			}
		}

		ArrayList<CoupleItem> itemList;

		public ItemModel() {
			super();
			itemList = new ArrayList<CoupleItem>();
		}

		public String getIdAt(int index) {
			if (index < itemList.size())
				return itemList.get(index).getId();
			else
				return null;
		}

		public int getSize() {
			return itemList.size();
		}

		public void addElement(String id, String name) {
			itemList.add(new CoupleItem(id, name));
		}

		public void removeAllData() {
			itemList.clear();
		}

	}

}
